/**
 * Created by Lenovo on 2017/9/14.
 */
$(function () {
    //Date picker
    $('#starttime').datepicker({
        autoclose: true,
        format: 'yyyy-mm-dd',
        defaults: new Date(),
        day: 1
     });


    $('#endtime').datepicker({
        showOtherMonths: true,
        format: 'yyyy-mm-dd',
        autoclose: true
    });

    $('#btn_create').click(function () {
        create();
    });

});


function create() {
    var div = $('#part').val();

    var tableName = $('#tablename').val().trim();
    var partitionName = $('#partitions').val().trim();
    var initial = $('#initial').val().trim();
    var next = $('#next').val().trim();
    var startTime = $('#starttime').val().trim();
    var endTime = $('#endtime').val().trim();
    isEmpty(tableName,'tableName');
    isEmpty(partitionName,'partitions');
    isEmpty(startTime,'starttime');
    isEmpty(endTime,'endtime');

    var startDate = getDate(startTime);
    startTime = formatDate(startDate);
    var endDate = getDate(endTime);
    endTime = formatDate(endDate);
    if(startDate > endDate){
        $.fn.modalAlert('end time cannot less than start time', 'danger');
        $('#endtime').val('');
        $('#endtime').focus();
        return;
    }
    var time = endDate.getTime() - startDate.getTime();
    var days = parseInt(time / (1000 * 60 * 60 * 24));
    // days = days>5?5:days;

    data = "tableName="+tableName+"&partitionName="+partitionName
        +"&initial="+initial+"&next="+next
        +"&startTime="+startTime+"&endTime="+endTime;
    $.ajax({
        url: "/morning/tools/createPartition",
        type: 'post',
        data: data,
        success: function (e) {
            $('#part').attr('rows',15);
            $('#part').val(e.msg);
            // $('#part').text(e.msg);
        },
        error: function () {
            $.fn.modalAlert('create error', 'danger');
            $('#todo').focus();
        }
    })
}
function formatDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth()<10?'0'+date.getMonth():date.getMonth();
    var day = date.getDate()<10?'0'+date.getDate():date.getDate();
    return ''+year+month+day;

}
function getDate(str) {
    //09/12/2017  mm/dd/yyyy
    var date = new Date();
    var strs = str.split("-");
    date.setFullYear(strs[2]);
    date.setMonth(strs[0]);
    date.setDate(strs[1]);
    return date;

}
function  isEmpty(content,id) {
    if (content == null || content == undefined || content == ''){
        $.fn.modalAlert('Please Fill The Content', 'danger');
        $('#id').val('');
        $('#id').focus();
        return;
    }
}